<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="@/assets/logo.png" alt />
      </div>
      <!-- 登录表单区域 -->
      <el-form :rules="loginRules" ref="loginForm" :model="loginForms"  label-width="0" class="login_form">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model="loginForms.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input type="password" v-model="loginForms.password" prefix-icon="iconfont icon-lock_fill"></el-input>
        </el-form-item>
        <!-- 登录&注册按钮 -->
        <el-form-item class="btns">
          <el-button @click="loginFn" type="primary">登录</el-button>
          <el-button @click="resetFn">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
//导入阿里字体图标
import "../assets/fonts/iconfont.css";
//导入登录请求API
import {loginAPI} from '@/api'

export default {
  data() {
    return {
      loginForms: {
        username: "admin",
        password: "123456"
      },
      //校验规则数组
      loginRules:{
         username:[
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
         ],
         password:[
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 5, max: 15, message: '长度在 5 到 15 个字符', trigger: 'blur' }
         ]
    }
  }
},
methods:{
  //重置表单数据方法
  resetFn(){
    this.$refs.loginForm.resetFields()
  },
  //登录校验方法
  loginFn(){
    console.log(this);
    //res是预检验是否成功的布尔值
    this.$refs.loginForm.validate( async res => {
      if(!res) return
      //将原表单数据卸给一个新对象
      const fromsData = {...this.loginForms}
      const {data} = await loginAPI(fromsData)
      if(data.meta.status !== 200) return this.msg.error(data.meta.msg)
      this.msg.success(data.meta.msg)
      //如果登录成功则在浏览器回话存储中保存登录成功后服务器返回的token值
      window.sessionStorage.setItem('token',data.data.token)
      //登陆成功后通过编程式导航跳转页面到首页
      //编程式导航$router是当前vue对象的路由管理器
      //$router的push方法可以切换path路径,query属性可以发出需要传递到目标页面的数据
      this.$router.push({
        path:'/home'
      })
    })
  }
}
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 30px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
